package com.guaoran.interview.in2018.basictypes;

import java.lang.reflect.Field;

/**
 * @author : guaoran
 * @Description : <br/>
 *  Integer 基本类型
 * @date :2018/12/21 8:43
 */
public class IntegerDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //new 的时候都是新的类型
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i==j);
        //Integer 有缓存，针对-128~127 进行缓存
        Integer a = 1,b = 1;
        System.out.println(a==b);
        Integer c = 128 ,d = 128;
        System.out.println(c==d);

        //交换Integer类型的值
        Integer f = 1,g = 2;
        System.out.println("before:f="+f+",g="+g);
        swap(f,g);
        System.out.println(" after:f="+f+",g="+g);

//        int x = 4,y = 5;
//        method(x,y);
//        System.out.println("x="+x+",y="+y);
    }

    private static void method(int a, int b) {
//        a = a ^ b;
//        b = a ^ b;
//        a = a ^ b;
    }

    /**
     * 交换Integer类型的值
     * @param f
     * @param g
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void swap(Integer f,Integer g) throws NoSuchFieldException, IllegalAccessException {
        Field field = Integer.class.getDeclaredField("value");
        field.setAccessible(true);
        Integer temp = new Integer(f.intValue());
        field.set(f,g.intValue());
        field.set(g,temp);
    }
}
